package org.mycub.server.parser;

import org.json.JSONException;
import org.json.JSONObject;
import org.mycub.server.data.models.FreeParking;
import org.mycub.server.data.models.InternetAccess;
import org.mycub.server.datainserter.InternetAccessesInserter;
import org.mycub.server.datainserter.factory.InternetAccessesInserterFactory;
import org.opengis.feature.simple.SimpleFeature;



public class CollectInternetAccess extends CollectItem{

	FreeParking parking;

	public CollectInternetAccess() {
		this.setFreeParking(new FreeParking());
	}

	public void collect(JSONObject json) {
		
		InternetAccess netAccess = new InternetAccess();
		
		try {
			String id = json.get("cle").toString();
			String nom = json.get("lieux").toString();
			String access = json.get("acces_libre").toString();
			String accueil = json.get("accueil").toString();
			String address = json.get("adresse").toString();
			String postCode = json.get("cp").toString();
			String free = json.get("gratuit").toString();
			String postsNb = json.get("nb_de_poste").toString();
			String postDis = json.get("poste_handi").toString();
			String ville = json.get("ville").toString();
			String lon = json.get("x_long").toString();
			String lat = json.get("y_lat").toString();
			
			String addressFull = address + " - " + postCode + " " + ville;
			
			netAccess.setId(id);
			netAccess.setName(nom);
			netAccess.setFreeAccess((access.equals("oui"))?true:false);
			netAccess.setAccueil(accueil);
			netAccess.setAddress(addressFull);
			netAccess.setFree((free.equals("oui"))?true:false);
			netAccess.setPostDis((!postDis.isEmpty())?Integer.valueOf(postDis):0);
			netAccess.setPostsNb(postsNb);
			netAccess.setLongitude((lon.isEmpty())?0:Double.valueOf(lon));
			netAccess.setLatitude((lat.isEmpty())?0:Double.valueOf(lat));
		
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		System.out.println("collected internet access : " + netAccess.getName() + 
				" id = " + netAccess.getId());

		saveInternetAccess(netAccess);
	}
	
	public void saveInternetAccess(InternetAccess netAccessCollected){
		InternetAccessesInserter netAccessesInserter = new InternetAccessesInserterFactory().createInternetAccessesInserter();
		netAccessesInserter.save(netAccessCollected);
	}

	public FreeParking getFreeParking() {
		return parking;
	}

	public void setFreeParking(FreeParking parking) {
		this.parking = parking;
	}

	@Override
	public void collect(SimpleFeature feature) {

	}	
}
